﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Classes;

namespace OfficeManager_v0._1
{
    public partial class AssignOffice : Form
    {
        Department mDepartment;
        Organization mOrganization;
        public AssignOffice(Organization aOrganization, Department aDepartment)
        {
            mOrganization = aOrganization;
            mDepartment = aDepartment;
            InitializeComponent();

            comboBoxOffice.DropDownStyle = ComboBoxStyle.DropDownList;
            PopulateComboBox();
        }

        private void PopulateComboBox()
        {
            comboBoxOffice.Enabled = false;
            foreach (Office office in mOrganization.Offices)
            {
                if (office.HostDepartment != null)
                    continue;

                comboBoxOffice.Enabled = true;
                comboBoxOffice.Items.Add(office.Name + " ID:" + office.ID);
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                Office target = mOrganization.Offices.Find((office) =>
                {
                    return office.Name + " ID:" + office.ID == comboBoxOffice.GetItemText(comboBoxOffice.SelectedItem);
                });

                target.HostDepartment = mDepartment;
                mDepartment.Offices.Add(target);
            }
            catch (Exception exc)
            {
                MessageBox.Show("Въведените данни за некоректни!");
            }

            this.Close();
            this.Dispose();
        }
    }
}
